/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.menosehmais.controller.rest;

import br.com.caelum.vraptor.Get;
import br.com.menosehmais.business.PortletBO;
import br.com.menosehmais.framework.security.UserSession;
import br.com.menosehmais.model.MarkedDO;
import br.com.menosehmais.model.PortletDO;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.Validator;
import br.com.menosehmais.model.RejectedDO;
import javax.servlet.http.HttpServletRequest;
import br.com.menosehmais.framework.presentation.AbstractRestController;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Resource;
import br.com.menosehmais.business.ClickPortletBO;
import br.com.menosehmais.business.MarkedBO;
import br.com.menosehmais.business.OpinionBO;
import br.com.menosehmais.business.RejectedBO;
import br.com.menosehmais.business.UsuarioBO;
import br.com.menosehmais.business.ViewPortletBO;
import br.com.menosehmais.dto.PortletDescriptionDTO;
import br.com.menosehmais.framework.persistence.Id;
import br.com.menosehmais.framework.security.annotation.PermitAll;
import br.com.menosehmais.model.UsuarioDO;

import static br.com.caelum.vraptor.view.Results.json;

/**
 *
 * @author Junior JAFJ
 */
@Resource
public class PortletController extends AbstractRestController {
    private ClickPortletBO clickPortletBO;
    private ViewPortletBO viewPortletBO;
    private MarkedBO markedBO;
    private RejectedBO rejectedBO;
    private OpinionBO opinioBO;
    private UserSession userSession;
    private UsuarioBO usuarioBO;
    private PortletBO portletBO;

    public PortletController(HttpServletRequest request, Result result, Validator validator,
            ClickPortletBO clickPortletBO, ViewPortletBO viewPortletBO,
            MarkedBO markedBO,
            RejectedBO rejectedBO,
            UserSession userSession,
            PortletBO portletBO,
            UsuarioBO usuarioBO,
            OpinionBO opinionBO) {
        super(request, result, validator);
        this.clickPortletBO = clickPortletBO;
        this.viewPortletBO = viewPortletBO;
        this.markedBO = markedBO;
        this.rejectedBO = rejectedBO;
        this.userSession = userSession;
        this.usuarioBO = usuarioBO;
        this.portletBO = portletBO;
        this.opinioBO = opinionBO;
    }

    @Get
    @Path("/rest/portlet/description/{id:" + Id.REGEX + "}")
    @PermitAll
    public void description(Id id) {
        PortletDescriptionDTO portletDescriptionDTO = new PortletDescriptionDTO();
        portletDescriptionDTO.setIdPortlet(id);
        PortletDO portletDO = portletDescriptionDTO.toDomainObject();
        portletDO = portletBO.selectPortlet(portletDO.getId());

        UsuarioDO usuarioDO = null;
        //TO-DO: Deve-se implementar esse código apos a devida definição do esquema de login
        if( userSession != null && userSession.getAccountInfo() != null && userSession.getAccountInfo().getAccountId() != null){
            //contaDO = contaBO.selectById(userSession.getAccountInfo().getAccountId());
        }
        usuarioDO = usuarioBO.selectById("9c87579c-0539-4aef-8f97-4efe65f42b44");//<<< TODO: Tire isso daqui!!!!
        
        if(usuarioDO != null){           
            portletDescriptionDTO.setClicks(clickPortletBO.getClicks(portletDescriptionDTO.toDomainObject(), usuarioDO));
            portletDescriptionDTO.setViews(viewPortletBO.getViews(usuarioDO));
            final MarkedDO marked = markedBO.getObjectMarked(portletDescriptionDTO.toDomainObject(), usuarioDO);
            portletDescriptionDTO.setMarked(marked.getPleasure());
            final RejectedDO rejected = rejectedBO.getObjectRejected(portletDescriptionDTO.toDomainObject(), usuarioDO);
            portletDescriptionDTO.setRejected(rejected.getPleasure());
            portletDescriptionDTO.setiLike(opinioBO.iLike(portletDO, usuarioDO));
            portletDescriptionDTO.setUrlImg(portletDO.getUrlImg());
            portletDescriptionDTO.setUrlLink(portletDO.getUrlLink());
        }

        result.use(json()).withoutRoot().from(portletDescriptionDTO).serialize();
    }
}
